<?php

namespace plugin\ky_black\app\admin\controller;

use support\Request;
use support\Response;
use plugin\ky_black\app\admin\model\MemberRights;

class MemberRightsController
{
    /**
     * 获取会员权益列表
     */
    public function list(Request $request)
    {
        $query = MemberRights::query();
        
        // 关键词搜索
        if ($keyword = $request->input('keyword')) {
            $query->where('name', 'like', "%{$keyword}%");
        }
        
        // 权益类型筛选
        if ($type = $request->input('rights_type')) {
            $query->where('rights_type', $type);
        }
        
        // 状态筛选
        if ($status = $request->input('status')) {
            $query->where('status', $status);
        }
        
        $list = $query->orderBy('sort', 'desc')
            ->orderBy('id', 'desc')
            ->paginate($request->input('limit', 15));
            
        return json_success('获取成功', $list);
    }

    /**
     * 获取选项数据
     */
    public function options()
    {
        $data = [
            'levels' => [
                1 => '普通会员',
                2 => '黄金会员',
                3 => '铂金会员',
                4 => '钻石会员'
            ]
        ];
        
        return json_success('获取成功', $data);
    }

    /**
     * 添加会员权益
     */
    public function add(Request $request)
    {
        $data = $request->post();
        
        $rights = new MemberRights();
        $rights->name = $data['name'];
        $rights->rights_type = $data['rights_type'];
        $rights->level = $data['level'];
        $rights->use_times = $data['use_times'];
        $rights->valid_period = $data['valid_period'];
        $rights->sort = $data['sort'] ?? 0;
        $rights->description = $data['description'] ?? '';
        $rights->use_rules = $data['use_rules'] ?? '';
        $rights->notice = $data['notice'] ?? '';
        $rights->status = $data['status'] ?? 1;
        
        if ($rights->save()) {
            return json_success('添加成功');
        }
        return json_error('添加失败');
    }

    /**
     * 编辑会员权益
     */
    public function edit(Request $request)
    {
        $data = $request->post();
        
        $rights = MemberRights::find($data['id']);
        if (!$rights) {
            return json_error('权益不存在');
        }
        
        $rights->name = $data['name'];
        $rights->rights_type = $data['rights_type'];
        $rights->level = $data['level'];
        $rights->use_times = $data['use_times'];
        $rights->valid_period = $data['valid_period'];
        $rights->sort = $data['sort'] ?? $rights->sort;
        $rights->description = $data['description'] ?? $rights->description;
        $rights->use_rules = $data['use_rules'] ?? $rights->use_rules;
        $rights->notice = $data['notice'] ?? $rights->notice;
        $rights->status = $data['status'] ?? $rights->status;
        
        if ($rights->save()) {
            return json_success('编辑成功');
        }
        return json_error('编辑失败');
    }

    /**
     * 删除会员权益
     */
    public function delete(Request $request)
    {
        $id = $request->input('id');
        
        if (MemberRights::destroy($id)) {
            return json_success('删除成功');
        }
        return json_error('删除失败');
    }
} 